home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zcolor.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  6.6 KB  |  263 lines

  1. /* Copyright (C) 1989, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zcolor.c,v 1.4 2000/09/19 19:00:52 lpd Exp $ */
  20. /* Color operators */
  21. #include "ghost.h"
  22. #include "oper.h"
  23. #include "estack.h"
  24. #include "ialloc.h"
  25. #include "igstate.h"
  26. #include "iutil.h"
  27. #include "store.h"
  28. #include "gxfixed.h"
  29. #include "gxmatrix.h"
  30. #include "gzstate.h"
  31. #include "gxdevice.h"
  32. #include "gxcmap.h"
  33. #include "icolor.h"
  34.  
  35. /* Imported from gsht.c */
  36. void gx_set_effective_transfer(P1(gs_state *));
  37.  
  38. /* Define the number of stack slots needed for zcolor_remap_one. */
  39. const int zcolor_remap_one_ostack = 4;
  40. const int zcolor_remap_one_estack = 3;
  41.  
  42. /* - currentgray <gray> */
  43. private int
  44. zcurrentgray(i_ctx_t *i_ctx_p)
  45. {
  46.     os_ptr op = osp;
  47.     float gray;
  48.     int code = gs_currentgray(igs, &gray);
  49.  
  50.     if (code < 0)
  51.     return code;
  52.     push(1);
  53.     make_real(op, gray);
  54.     return 0;
  55. }
  56.  
  57. /* - currentrgbcolor <red> <green> <blue> */
  58. private int
  59. zcurrentrgbcolor(i_ctx_t *i_ctx_p)
  60. {
  61.     os_ptr op = osp;
  62.     float par[3];
  63.     int code = gs_currentrgbcolor(igs, par);
  64.  
  65.     if (code < 0)
  66.     return code;
  67.     push(3);
  68.     make_floats(op - 2, par, 3);
  69.     return 0;
  70. }
  71.  
  72. /* - currenttransfer <proc> */
  73. private int
  74. zcurrenttransfer(i_ctx_t *i_ctx_p)
  75. {
  76.     os_ptr op = osp;
  77.  
  78.     push(1);
  79.     *op = istate->transfer_procs.colored.gray;
  80.     return 0;
  81. }
  82.  
  83. /* - processcolors <int> - */
  84. /* Note: this is an undocumented operator that is not supported */
  85. /* in Level 2. */
  86. private int
  87. zprocesscolors(i_ctx_t *i_ctx_p)
  88. {
  89.     os_ptr op = osp;
  90.  
  91.     push(1);
  92.     make_int(op, gs_currentdevice(igs)->color_info.num_components);
  93.     return 0;
  94. }
  95.  
  96. /* <gray> setgray - */
  97. private int
  98. zsetgray(i_ctx_t *i_ctx_p)
  99. {
  100.     os_ptr op = osp;
  101.     double gray;
  102.     int code;
  103.  
  104.     if (real_param(op, &gray) < 0)
  105.     return_op_typecheck(op);
  106.     if ((code = gs_setgray(igs, gray)) < 0)
  107.     return code;
  108.     make_null(&istate->colorspace.array);
  109.     pop(1);
  110.     return 0;
  111. }
  112.  
  113. /* <red> <green> <blue> setrgbcolor - */
  114. private int
  115. zsetrgbcolor(i_ctx_t *i_ctx_p)
  116. {
  117.     os_ptr op = osp;
  118.     double par[3];
  119.     int code;
  120.  
  121.     if ((code = num_params(op, 3, par)) < 0 ||
  122.     (code = gs_setrgbcolor(igs, par[0], par[1], par[2])) < 0
  123.     )
  124.     return code;
  125.     make_null(&istate->colorspace.array);
  126.     pop(3);
  127.     return 0;
  128. }
  129.  
  130. /* <proc> settransfer - */
  131. private int
  132. zsettransfer(i_ctx_t *i_ctx_p)
  133. {
  134.     os_ptr op = osp;
  135.     int code;
  136.  
  137.     check_proc(*op);
  138.     check_ostack(zcolor_remap_one_ostack - 1);
  139.     check_estack(1 + zcolor_remap_one_estack);
  140.     istate->transfer_procs.colored.red =
  141.     istate->transfer_procs.colored.green =
  142.     istate->transfer_procs.colored.blue =
  143.     istate->transfer_procs.colored.gray = *op;
  144.     code = gs_settransfer_remap(igs, gs_mapped_transfer, false);
  145.     if (code < 0)
  146.     return code;
  147.     push_op_estack(zcolor_reset_transfer);
  148.     pop(1);
  149.     return zcolor_remap_one(i_ctx_p, &istate->transfer_procs.colored.gray,
  150.                 igs->set_transfer.colored.gray, igs,
  151.                 zcolor_remap_one_finish);
  152. }
  153.  
  154. /* ------ Internal routines ------ */
  155.  
  156. /* Prepare to remap one color component */
  157. /* (also used for black generation and undercolor removal). */
  158. /* Use the 'for' operator to gather the values. */
  159. /* The caller must have done the necessary check_ostack and check_estack. */
  160. int
  161. zcolor_remap_one(i_ctx_t *i_ctx_p, const ref * pproc,
  162.          gx_transfer_map * pmap, const gs_state * pgs,
  163.          op_proc_t finish_proc)
  164. {
  165.     os_ptr op;
  166.  
  167.     /*
  168.      * Detect the identity function, which is a common value for one or
  169.      * more of these functions.
  170.      */
  171.     if (r_size(pproc) == 0) {
  172.     gx_set_identity_transfer(pmap);
  173.     /*
  174.      * Even though we don't actually push anything on the e-stack, all
  175.      * clients do, so we return o_push_estack in this case.  This is
  176.      * needed so that clients' finishing procedures will get run.
  177.      */
  178.     return o_push_estack;
  179.     }
  180.     op = osp += 4;
  181.     make_int(op - 3, 0);
  182.     make_int(op - 2, 1);
  183.     make_int(op - 1, transfer_map_size - 1);
  184.     *op = *pproc;
  185.     ++esp;
  186.     make_struct(esp, imemory_space((gs_ref_memory_t *) pgs->memory),
  187.         pmap);
  188.     push_op_estack(finish_proc);
  189.     push_op_estack(zfor_fraction);
  190.     return o_push_estack;
  191. }
  192.  
  193. /* Store the result of remapping a component. */
  194. private int
  195. zcolor_remap_one_store(i_ctx_t *i_ctx_p, floatp min_value)
  196. {
  197.     int i;
  198.     gx_transfer_map *pmap = r_ptr(esp, gx_transfer_map);
  199.  
  200.     if (ref_stack_count(&o_stack) < transfer_map_size)
  201.     return_error(e_stackunderflow);
  202.     for (i = 0; i < transfer_map_size; i++) {
  203.     double v;
  204.     int code =
  205.         real_param(ref_stack_index(&o_stack, transfer_map_size - 1 - i),
  206.                &v);
  207.  
  208.     if (code < 0)
  209.         return code;
  210.     pmap->values[i] =
  211.         (v < min_value ? float2frac(min_value) :
  212.          v >= 1.0 ? frac_1 :
  213.          float2frac(v));
  214.     }
  215.     ref_stack_pop(&o_stack, transfer_map_size);
  216.     esp--;            /* pop pointer to transfer map */
  217.     return o_pop_estack;
  218. }
  219. int
  220. zcolor_remap_one_finish(i_ctx_t *i_ctx_p)
  221. {
  222.     return zcolor_remap_one_store(i_ctx_p, 0.0);
  223. }
  224. int
  225. zcolor_remap_one_signed_finish(i_ctx_t *i_ctx_p)
  226. {
  227.     return zcolor_remap_one_store(i_ctx_p, -1.0);
  228. }
  229.  
  230. /* Finally, reset the effective transfer functions and */
  231. /* invalidate the current color. */
  232. int
  233. zcolor_reset_transfer(i_ctx_t *i_ctx_p)
  234. {
  235.     gx_set_effective_transfer(igs);
  236.     return zcolor_remap_color(i_ctx_p);
  237. }
  238. int
  239. zcolor_remap_color(i_ctx_t *i_ctx_p)
  240. {
  241.     gx_unset_dev_color(igs);
  242.     return 0;
  243. }
  244.  
  245. /* ------ Initialization procedure ------ */
  246.  
  247. const op_def zcolor_op_defs[] =
  248. {
  249.     {"0currentgray", zcurrentgray},
  250.     {"0currentrgbcolor", zcurrentrgbcolor},
  251.     {"0currenttransfer", zcurrenttransfer},
  252.     {"0processcolors", zprocesscolors},
  253.     {"1setgray", zsetgray},
  254.     {"3setrgbcolor", zsetrgbcolor},
  255.     {"1settransfer", zsettransfer},
  256.         /* Internal operators */
  257.     {"1%zcolor_remap_one_finish", zcolor_remap_one_finish},
  258.     {"1%zcolor_remap_one_signed_finish", zcolor_remap_one_signed_finish},
  259.     {"0%zcolor_reset_transfer", zcolor_reset_transfer},
  260.     {"0%zcolor_remap_color", zcolor_remap_color},
  261.     op_def_end(0)
  262. };
  263.